<?php

namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台首页控制器
 */
class ItemController extends AdminBaseController {
    /**
     * 首页
     */
    public function index() {
        $itemList = M("item")->order('item_sort asc')->where(array('status' => 1))->select();
        //        foreach ($itemList as $k=>$item){
        //            if(!$item['item_image']){
        //                $itemList[$k]['item_image'] = "null.jpg";
        //            }
        //        }
        foreach ($itemList as $k => $item) {
            $itemList[$k]['item_type_name'] = getItemTypeName($item['item_type']);
            $itemList[$k]['item_image_small'] = getSmallImage($item['item_image']);
        }
        $itemList2 = M("item_class")->order("item_type_sort asc")->select();
        $this->assign('itemList', $itemList);
        $this->assign('itemTypeList', $itemList2);
        $this->display();
    }

    public function addClass() {
        $class_name = trim($_POST['class_name']);
        $itemInfo = M("item_class")->where(array('item_type_name' => $class_name))->find();
        if (!empty($itemInfo)) {
            $this->error("添加失败，类别名称重复");
        }
        else {
            $inserDate = array();
            $inserDate['item_type_name'] = $class_name;
            $res = M("item_class")->add($inserDate);
            if ($res) {
                $this->success("添加类别成功");
            }
            else {
                $this->error("添加失败，请联系管理员");

            }
        }
        exit;
    }
    public function itemClass(){
        $itemTypeList = M('item_class')->order('item_type_sort asc')->select();
        $this->assign('itemTypeList', $itemTypeList);
        $this->display();
    }
    public function saveClassSort(){
        $sortList = $_POST['sort'];
        if(!empty($sortList)){
            foreach ($sortList as $class_id=>$sort){
                $data = array();
                $data['item_type_sort'] = !$sort?255:$sort;
                M("item_class")->where(array('item_type_id'=>$class_id))->save($data);
            }
            $this->success("保存成功");
        }else{
            $this->error("非法请求");
        }

    }

    public function add() {
        $itemInfo = array();
        $itemInfo['item_name'] = trim($_POST['item_name']);
        $itemInfo['unit_name'] = trim($_POST['item_unit_name']);
        $itemInfo['item_type'] = trim($_POST['item_type']);
        $itemInfo['addtime'] = time();
        if (!empty($_FILES['item_image']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728;// 设置附件上传大小
            $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Public/images/'; // 设置附件上传根目录
            // 上传单个文件
            $info = $upload->uploadOne($_FILES['item_image']);
            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }
            $itemInfo['item_image'] = $info['savepath'] . $info['savename'];

            $image = new \Think\Image();
            $fileName = $_SERVER['DOCUMENT_ROOT'].'/Public/images/'.$itemInfo['item_image'];
            $fileNamell = explode("/",$itemInfo['item_image']);
            $fileNamell = explode(".",$fileNamell[1]);
            $fileNamell = $fileNamell[0];
            $finder = explode('/',$itemInfo['item_image']);
            $finder = $finder[0];
            $sadasdFileName = $_SERVER['DOCUMENT_ROOT'].'/Public/images/'.$finder."/".$fileNamell."_s.jpeg";
            $image->open($fileName);
            $image->thumb(150, 150)->save($sadasdFileName);

        }

        $res = M("item")->add($itemInfo);
        if ($res) {
            $this->success('添加成功', U('Admin/Item/index/'));
        }
        else {
            $this->error('添加失败，请联系管理员');
        }
    }

    public function del() {
        $item_id = intval($_POST['item_id']);
        if (!$item_id) {
            returnJson(false, array(), '操作失败');
        }
        else {
            $updataArr = array();
            $updataArr['item_id'] = $item_id;
            $updataArr['status'] = 0;
            $res = M("item")->save($updataArr);
            if ($res) {
                returnJson(true, array(), '操作成功');

            }
            else {
                returnJson(false, array(), '操作失败');

            }
        }
    }

    /**
     * elements
     */
    public function edit() {
        $item_id = $_GET['item_id'];
        $itemInfo = M("item")->where(array('item_id' => $item_id))->find();
        returnJson(true, $itemInfo, '');
    }

    public function saveEditItem() {

        $updateArr = array();
        $updateArr['item_id'] = $_POST['item_id'];
        $updateArr['item_name'] = trim($_POST['item_name']);
        $updateArr['unit_name'] = trim($_POST['item_unit_name']);
        $updateArr['item_type'] = trim($_POST['item_type']);
        if (!empty($_FILES['item_image']['name'])) {
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize = 3145728;// 设置附件上传大小
            $upload->exts = array('jpg', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath = './Public/images/'; // 设置附件上传根目录
            // 上传单个文件
            $info = $upload->uploadOne($_FILES['item_image']);
            if (!$info) {// 上传错误提示错误信息
                $this->error($upload->getError());
            }
            $updateArr['item_image'] = $info['savepath'] . $info['savename'];



            $image = new \Think\Image();
            $fileName = $_SERVER['DOCUMENT_ROOT'].'/Public/images/'.$updateArr['item_image'];
            $fileNamell = explode("/",$updateArr['item_image']);
            $fileNamell = explode(".",$fileNamell[1]);
            $fileNamell = $fileNamell[0];
            $finder = explode('/',$updateArr['item_image']);
            $finder = $finder[0];
            $sadasdFileName = $_SERVER['DOCUMENT_ROOT'].'/Public/images/'.$finder."/".$fileNamell."_s.jpeg";
            $image->open($fileName);
            $image->thumb(150, 150)->save($sadasdFileName);

        }

        $res = M("item")->save($updateArr);
        if ($res) {
            $this->success('修改成功', U('Admin/Item/index/'));

        }
        else {
            $this->error('修改失败，请联系管理员');

        }
    }

    /**
     * welcome
     */
    public function welcome() {
        $this->display();
    }

    public function saveSort() {
        $list = $_POST['sort'];
        if (!empty($list)) {
            foreach ($list as $item_id => $l) {
                $update = array();
                $update['item_id'] = $item_id;
                $update['item_sort'] = !$l?255:$l;
                M("item")->save($update);

            }
            $this->success("已更新菜品排序");
        }
        exit;
    }


}
